package com.sk.cardeal.utils;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sk.cardeal.app.MyApplication;
import com.sk.cardeal.callback.HandleDataCallBack;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.LinkedHashMap;
import java.util.Map;

public class HttpUtils {

    public static void postData(String urlStr, final LinkedHashMap<String, Object> args, final HandleDataCallBack callBack, final int tag, Activity activity) {
        String url = HttpAddress.TestIp + urlStr;
        RequestParams params = new RequestParams(url);
        for (Object o : args.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String val = entry.getValue().toString();
            String key = entry.getKey().toString();
            params.addBodyParameter(key, val);
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("onSuccess", result);
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(result);
                int code = jsonObject.getIntValue("returnCode");
                if (code == 1) {
                    callBack.handlerSuccessData(result, tag);
                } else {
                    callBack.handlerFailData(result, tag);
                    Toast.makeText(MyApplication.getAppContext(), jsonObject.getString("returnMsg"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("onError", ex.getMessage());
                callBack.handleErrorData(ex.getMessage(), tag);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    static void getData(String urlStr, final LinkedHashMap<String, Object> args, final HandleDataCallBack callBack, final int tag, Activity activity) {
        String url = HttpAddress.TestIp + urlStr;
        RequestParams params = new RequestParams(url);
        for (Object o : args.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String val = entry.getValue().toString();
            String key = entry.getKey().toString();
            params.addBodyParameter(key, val);
        }
        Log.v("getData", params.toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("onSuccess", result);
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(result);
                int code = jsonObject.getIntValue("returnCode");
                if (code == 1) {
                    callBack.handlerSuccessData(result, tag);
                } else {
                    callBack.handlerFailData(result, tag);
                    Toast.makeText(MyApplicationLike.getAppContext(), jsonObject.getString("returnMsg"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("onError", ex.toString());
                callBack.handleErrorData(ex.toString(), tag);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    static void patchData(String urlStr, final LinkedHashMap<String, Object> args, final HandleDataCallBack callBack, final int tag, Activity activity) {
        String url = HttpAddress.TestIp + urlStr;
        for (Object o : args.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String val = entry.getValue().toString();
            String key = entry.getKey().toString();
            if (val.length() != 0) {
                url = url + "/" + val;
            } else {
                url = url + "/undefined";
            }

        }
        RequestParams params = new RequestParams(url);
        Log.v("getData", params.toString());
        x.http().request(HttpMethod.GET, params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("onSuccess", result);
                JSONObject jsonObject = JSON.parseObject(result);
                int code = jsonObject.getIntValue("returnCode");
                if (code == 1) {
                    callBack.handlerSuccessData(result, tag);
                } else {
                    callBack.handlerFailData(result, tag);
                    Toast.makeText(MyApplicationLike.getAppContext(), jsonObject.getString("returnMsg"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("onError", ex.toString());
                callBack.handleErrorData(ex.toString(), tag);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }


    static void postJSONData(String urlStr, LinkedHashMap map, final HandleDataCallBack callBack, final int tag, Activity activity) {
        String url = HttpAddress.TestIp + urlStr;
        RequestParams params = new RequestParams(url);
        params.setAsJsonContent(true);
        params.setBodyContent(JSON.toJSONString(map.get("jsonString")));
        Log.v("postJSONData",JSON.toJSONString(map.get("jsonString")));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("onSuccess", result);
                JSONObject jsonObject = JSON.parseObject(result);
                int code = jsonObject.getIntValue("returnCode");
                if (code == 1) {
                    callBack.handlerSuccessData(result, tag);
                } else {
                    callBack.handlerFailData(result, tag);
                    Toast.makeText(MyApplicationLike.getAppContext(), jsonObject.getString("returnMsg"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("onError", ex.toString());
                callBack.handleErrorData(ex.toString(), tag);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    static void postPayJson(String urlStr, LinkedHashMap map, final HandleDataCallBack callBack, final int tag, Activity activity) {
        String url = HttpAddress.TestIp + urlStr;
        RequestParams params = new RequestParams(url);
        params.setAsJsonContent(true);
        params.setBodyContent(JSON.toJSONString(map.get("jsonString")));
        Log.v("postJSONData",JSON.toJSONString(map.get("jsonString")));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("onSuccess", result);
                JSONObject jsonObject = JSON.parseObject(result);
                int code = jsonObject.getIntValue("returnCode");
                if (code !=0 && code !=100) {
                    callBack.handlerSuccessData(result, tag);
                } else if(code == 0){
                    callBack.handlerFailData(result, tag);
                    Toast.makeText(MyApplicationLike.getAppContext(), jsonObject.getString("returnMsg"), Toast.LENGTH_SHORT).show();
                }else if(code == 100){
                    callBack.handlerFailData(result, tag);
                    Toast.makeText(MyApplicationLike.getAppContext(), "该笔订单已支付！", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("onError", ex.toString());
                callBack.handleErrorData(ex.toString(), tag);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

}
